package mns;

import java.io.*;
import java.util.*;
import java.util.zip.*;

public class UnZip
{
	static final int BUFFER = 2048;

	public static void unzip(String file, String destFolder)
	{
		try
		{
			BufferedOutputStream dest = null;
			BufferedInputStream is = null;
			ZipEntry entry;
			ZipFile zipfile = new ZipFile(file);
			Enumeration e = zipfile.entries();
			while(e.hasMoreElements())
			{
				entry = (ZipEntry)e.nextElement();
				if(entry.isDirectory())
				{
					String dir = destFolder + File.pathSeparator + entry.getName();
					System.out.println("Creating directory: " + dir);
					File f = new File(dir);
					f.mkdirs();
				}
				else
				{
					System.out.println("Extracting: " + entry);
					is = new BufferedInputStream(zipfile.getInputStream(entry));
					int count;
					byte data[] = new byte[BUFFER];
					
					String path = destFolder + File.pathSeparator + entry.getName();
					FileOutputStream fos = new FileOutputStream(path);
					dest = new BufferedOutputStream(fos, BUFFER);
					while((count = is.read(data, 0, BUFFER)) != -1)
					{
						dest.write(data, 0, count);
					}
					dest.flush();
					dest.close();
					is.close();
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
